package com.zzy.python;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class RunPythonDemo {

	public static final String FILE_NAME = "now_func.py";
	
	public static String runPyScript(String day) {
		StringBuffer sb = new StringBuffer();
        Process proc = null;
        try {
        	String pythonFilePath = System.getProperty("python.filePath");
        	if(null== pythonFilePath) {
        		
        	}
        	String path = pythonFilePath+ File.separatorChar + FILE_NAME;
            String[] args1 = new String[] { "python", path, String.valueOf(day) };
            proc = Runtime.getRuntime().exec(args1);// 执行py文件
            //用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GB2312"));
            String line = null;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
        	if(null != proc) {
            	proc.destroy();
        	}
		}
		return sb.toString(); 
	}
	

	public static void main(String[] args) {
		System.setProperty("python.filePath", "D:\\python\\" );
		String day = "2023-12-09";
		String dto = RunPythonDemo.runPyScript(day);
		System.out.println(dto);
	}


}
